﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Xml.Schema;
using System.Windows.Forms;
//对XML根据XSD进行验证
namespace MyXMLPrase
{
    class ValidateXML
    {
        private XmlValidatingReader myXmlValidatingReader = null;
        private XmlTextReader myXmlTextReader = null;
        private Boolean Success = true;
         ValidateXMLInfomationDisplay vd = new ValidateXMLInfomationDisplay();
         
        
        public void Run(String file)
        {
            vd.richTextBox1.Text = "Start:\n";
           try
            {


                XmlSchemaCollection myXmlSchemaCollection = new XmlSchemaCollection();
                myXmlSchemaCollection.Add(null, "06-11-06.xsd");
                //   用架构验证   XML   文件 
                Success = true;
                //Console.WriteLine();
                //Console.WriteLine("正在用架构文件  06-11-06.xsd  验证   XML   文件   " + file.ToString() + "   ... ");
               vd.richTextBox1.Text += "\n正在用架构文件  06-11-06.xsd  验证   XML   文件   " + file.ToString() + "   ... ";
                myXmlTextReader = new XmlTextReader(file);
                myXmlValidatingReader = new XmlValidatingReader(myXmlTextReader);
                myXmlValidatingReader.Schemas.Add(myXmlSchemaCollection);
                myXmlValidatingReader.ValidationType = ValidationType.Schema;
                Validate();


            }

            catch (Exception e)
            {
               // MessageBox.Show("异常: " + e.ToString()); 
               // Console.WriteLine("异常： " + e.ToString());
                vd.richTextBox1.Text += "\n异常： " + e.ToString();
            }

            finally
            {
                //   通过   XmlTextReader   完成 
                if (myXmlValidatingReader != null)
                    myXmlValidatingReader.Close();
            }
           vd.Visible = true;

        }

        private void Validate()
        {
            try
            {
                //   设置验证事件处理程序 
                myXmlValidatingReader.ValidationEventHandler += new ValidationEventHandler(this.ValidationEventHandle);

                //   读取   XML   数据 
                while (myXmlValidatingReader.Read()) { }

                //Console.WriteLine();
                //Console.BackgroundColor = ConsoleColor.Blue;
                //Console.ForegroundColor = ConsoleColor.Green;
                //Console.WriteLine("==>验证已完成。验证{0}", (Success == true ? "成功 " : "失败 "));
                //Console.BackgroundColor = ConsoleColor.Black;
               
                vd.richTextBox1.Text += "\n==>验证已完成。验证:" + (Success == true ? "成功 " : "失败 ");
            }
            catch (XmlException e)
            {
              //  MessageBox.Show("XML 异常;" + e.ToString());
                //Console.WriteLine("Xml   异常： " + e.ToString());
                vd.richTextBox1.Text += "\nXml   异常： " + e.ToString();
            }

            catch (Exception e)
            {
                //MessageBox.Show("异常 :" + e.ToString());
                //Console.WriteLine("异常 :" + e.ToString());
                vd.richTextBox1.Text += "\n异常 :" + e.ToString();
            }
        }

        public void ValidationEventHandle(object sender, ValidationEventArgs args)
        {
            Success = false;

            //Console.BackgroundColor = ConsoleColor.White;
            //Console.ForegroundColor = ConsoleColor.Red;
            //Console.WriteLine("\n==验证错误：{0}", args.Message);
            //Console.ForegroundColor = ConsoleColor.White;
            //Console.BackgroundColor = ConsoleColor.Black;

            vd.richTextBox1.Text += "\n==验证错误："+ args.Message;

            if (args.Severity == XmlSeverityType.Warning)
            {
                //MessageBox.Show("未找到要强制验证的架构");
                //Console.WriteLine("未找到要强制验证的架构。 ");
                vd.richTextBox1.Text += "\n未找到要强制验证的架构\n ";
                
            }
            else
            {
                if (args.Severity == XmlSeverityType.Error)
                {
                    //MessageBox.Show("验证实例文档时发生验证错误");
                    //Console.WriteLine("验证实例文档时发生验证错误。-- ");
                    vd.richTextBox1.Text += "\n验证实例文档时发生验证错误";
                }
            }

            if (args.Exception != null)   //   XSD   架构验证错误 
            {
                //MessageBox.Show(args.Exception.SourceUri + ", " + args.Exception.LinePosition + ", " + args.Exception.LineNumber);
               // Console.WriteLine(args.Exception.SourceUri + ", " + args.Exception.LinePosition + ", " + args.Exception.LineNumber);
                vd.richTextBox1.Text += "\n"+args.Exception.SourceUri + ",\n " + args.Exception.LinePosition + ",\n " + args.Exception.LineNumber;
            }

            vd.richTextBox1.Text += "\n\n";
        }
    }
}
